package com.softtek.base.business.dao.impl;

import com.softtek.base.business.dao.QuestionTwoDao;
import com.softtek.base.domain.QuestionTwo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Created by li.chen on 8/19/2015.
 */
@Repository("questionTwoDao")
public class QuestionTwoDaoImpl implements QuestionTwoDao {

    private final static Logger logger = LoggerFactory.getLogger(QuestionTwoDaoImpl.class);

    @PersistenceContext
    private EntityManager em;

    @Override
    public Integer add(QuestionTwo questionTwo) {
        em.persist(questionTwo);
        logger.info("QuestionTwoDaoImpl添加questionTwo成功！");
        return 1;
    }

    @Override
    public Integer update(QuestionTwo questionTwo,String questionFourAnswer) {
        QuestionTwo existQuestionTwo = em.find(QuestionTwo.class, questionTwo.getQuestionTwoId());
        //问题答案图片Q2
        existQuestionTwo.setQuestionTwoAnswer(questionTwo.getQuestionTwoAnswer());
        existQuestionTwo.setQuestionTwoTypeA(questionTwo.getQuestionTwoTypeA());
        if (StringUtils.isNotBlank(questionTwo.getQuestionTwoTypeA())) {
            existQuestionTwo.setQuestionTwoNumA(questionTwo.getQuestionTwoNumA());
        }
        existQuestionTwo.setQuestionTwoImageUrlA(questionTwo.getQuestionTwoImageUrlA());
        existQuestionTwo.setQuestionTwoImageUrlA2(questionTwo.getQuestionTwoImageUrlA2());
        existQuestionTwo.setQuestionTwoImageUrlA3(questionTwo.getQuestionTwoImageUrlA3());

        existQuestionTwo.setQuestionTwoTypeB(questionTwo.getQuestionTwoTypeB());
        if (StringUtils.isNotBlank(questionTwo.getQuestionTwoTypeB())) {
            existQuestionTwo.setQuestionTwoNumB(questionTwo.getQuestionTwoNumB());
        }
        existQuestionTwo.setQuestionTwoImageUrlB(questionTwo.getQuestionTwoImageUrlB());
        existQuestionTwo.setQuestionTwoImageUrlB2(questionTwo.getQuestionTwoImageUrlB2());
        existQuestionTwo.setQuestionTwoImageUrlB3(questionTwo.getQuestionTwoImageUrlB3());

        existQuestionTwo.setQuestionTwoTypeC(questionTwo.getQuestionTwoTypeC());
        if (StringUtils.isNotBlank(questionTwo.getQuestionTwoTypeC())) {
            existQuestionTwo.setQuestionTwoNumC(questionTwo.getQuestionTwoNumC());
        }
        existQuestionTwo.setQuestionTwoImageUrlC(questionTwo.getQuestionTwoImageUrlC());
        existQuestionTwo.setQuestionTwoImageUrlC2(questionTwo.getQuestionTwoImageUrlC2());
        existQuestionTwo.setQuestionTwoImageUrlC3(questionTwo.getQuestionTwoImageUrlC3());

        existQuestionTwo.setQuestionTwoTypeD(questionTwo.getQuestionTwoTypeD());
        if (StringUtils.isNotBlank(questionTwo.getQuestionTwoTypeD())) {
            existQuestionTwo.setQuestionTwoNumD(questionTwo.getQuestionTwoNumD());
        }
        existQuestionTwo.setQuestionTwoImageUrlD(questionTwo.getQuestionTwoImageUrlD());
        existQuestionTwo.setQuestionTwoImageUrlD2(questionTwo.getQuestionTwoImageUrlD2());
        existQuestionTwo.setQuestionTwoImageUrlD3(questionTwo.getQuestionTwoImageUrlD3());

        existQuestionTwo.setQuestionTwoTypeE(questionTwo.getQuestionTwoTypeE());
        if (StringUtils.isNotBlank(questionTwo.getQuestionTwoTypeE())) {
            existQuestionTwo.setQuestionTwoNumE(questionTwo.getQuestionTwoNumE());
        }
        existQuestionTwo.setQuestionTwoImageUrlE(questionTwo.getQuestionTwoImageUrlE());
        existQuestionTwo.setQuestionTwoImageUrlE2(questionTwo.getQuestionTwoImageUrlE2());
        existQuestionTwo.setQuestionTwoImageUrlE3(questionTwo.getQuestionTwoImageUrlE3());

        existQuestionTwo.setQuestionTwoTypeG(questionTwo.getQuestionTwoTypeG());
        if (StringUtils.isNotBlank(questionTwo.getQuestionTwoTypeG())) {
            existQuestionTwo.setQuestionTwoNumG(questionTwo.getQuestionTwoNumG());
        }
        existQuestionTwo.setQuestionTwoImageUrlG(questionTwo.getQuestionTwoImageUrlG());
        existQuestionTwo.setQuestionTwoImageUrlG2(questionTwo.getQuestionTwoImageUrlG2());
        existQuestionTwo.setQuestionTwoImageUrlG3(questionTwo.getQuestionTwoImageUrlG3());

        existQuestionTwo.setQuestionTenAnswerImageUrl(questionTwo.getQuestionTenAnswerImageUrl());
        existQuestionTwo.setQuestionTenAnswerImageUrl2(questionTwo.getQuestionTenAnswerImageUrl2());
        existQuestionTwo.setQuestionTenAnswerImageUrl3(questionTwo.getQuestionTenAnswerImageUrl3());

        existQuestionTwo.setQuestionElevenAnswerImageUrl(questionTwo.getQuestionElevenAnswerImageUrl());
        existQuestionTwo.setQuestionElevenAnswerImageUrl2(questionTwo.getQuestionElevenAnswerImageUrl2());
        existQuestionTwo.setQuestionElevenAnswerImageUrl3(questionTwo.getQuestionElevenAnswerImageUrl2());

        //问题答案图片Q10
        existQuestionTwo.setQuestionTenAnswer(questionTwo.getQuestionTenAnswer());
        //问题答案图片Q11
        existQuestionTwo.setQuestionElevenAnswer(questionTwo.getQuestionElevenAnswer());
        //问题Q4.1的答案和图片
        existQuestionTwo.setQuestionFourOneAnswer(questionTwo.getQuestionFourOneAnswer());
        existQuestionTwo.setQuestionFourOnePicture(questionTwo.getQuestionFourOnePicture());
        existQuestionTwo.setQuestionFourOnePicture2(questionTwo.getQuestionFourOnePicture2());
        existQuestionTwo.setQuestionFourOnePicture3(questionTwo.getQuestionFourOnePicture3());
        existQuestionTwo.setQuestionFourOnePicture4(questionTwo.getQuestionFourOnePicture4());
        logger.info("questionFourAnswer:"+questionFourAnswer);
        if (StringUtils.isNotBlank(questionFourAnswer) && questionFourAnswer.equals("2")){
            existQuestionTwo.setQuestionFourOneAnswerA("2");
            existQuestionTwo.setQuestionFourOneAnswerB("2");
            existQuestionTwo.setQuestionFourOneAnswerC("2");
            existQuestionTwo.setQuestionFourOneAnswerD("2");
            existQuestionTwo.setQuestionFourOneAnswerANum("");
            existQuestionTwo.setQuestionFourOneAnswerBNum("");
        }
        else {
            existQuestionTwo.setQuestionFourOneAnswerA(questionTwo.getQuestionFourOneAnswerA());
            existQuestionTwo.setQuestionFourOneAnswerANum(questionTwo.getQuestionFourOneAnswerANum());
            existQuestionTwo.setQuestionFourOneAnswerB(questionTwo.getQuestionFourOneAnswerB());
            existQuestionTwo.setQuestionFourOneAnswerBNum(questionTwo.getQuestionFourOneAnswerBNum());
            existQuestionTwo.setQuestionFourOneAnswerC(questionTwo.getQuestionFourOneAnswerC());
            existQuestionTwo.setQuestionFourOneAnswerD(questionTwo.getQuestionFourOneAnswerD());
        }

        existQuestionTwo.setCanUserDepartmentAnswer(questionTwo.getCanUserDepartmentAnswer());
        existQuestionTwo.setCanUserDepartmentImage1(questionTwo.getCanUserDepartmentImage1());
        existQuestionTwo.setCanUserDepartmentImage2(questionTwo.getCanUserDepartmentImage2());
        if (StringUtils.isNotBlank(questionTwo.getCanUserDepartmentAnswer()) && questionTwo.getCanUserDepartmentAnswer().equals("1")){
            existQuestionTwo.setCanUserDepartmentInfo("");
        }
        else {
            existQuestionTwo.setCanUserDepartmentInfo(questionTwo.getCanUserDepartmentInfo());
        }

        return 1;
    }
}
